﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using EzDesk.MobileWeb.ViewModel;
using EzDesk.Core.CoreImpl;
using EzDesk.Core.Model;
using EzDesk.Core;
using EzDesk.Client.Utility;
using System.Text;
using EzDesk.Utility;

namespace EzDesk.MobileWeb.Controllers
{
    [HandleError]
    public class HomeController : EzControlloerBase
    {

        [Authorize]
        public ActionResult Index()
        {
            if (CurrentUserProfile == null)
            {
                return RedirectToAction("EditProfile");
            }

            // 获得最新的几条Notifications
            var notisData = _coreClientService.GetXmlFeedsByCount("notification", CurrentUserId, 0, 5, null, null);

            // 获取第一页Update
            var updatesData = _coreClientService.GetXmlFeedsByCount("update", CurrentUserId, 0, 20, null, null);

            FeedCollection notifications = transformXmlToHtml(notisData);
            FeedCollection updates = transformXmlToHtml(updatesData);

            ViewData["notifications"] = notifications;
            ViewData["updates"] = updates;

            ViewData["currentUser"] = CurrentSimpleUserInfo;
            return View();
        }

        [Authorize]
        public ActionResult AppTask(string appName)
        {
            ViewData["app"] = _coreClientService.GetAppByName(appName);
            ViewData["tasks"] = _coreClientService.GetAppTasksByAppName(appName, CurrentUserId, "GenericMobile");
            return View();
        }

        //
        // GET: /Home/EditProfile
        [Authorize]
        public ActionResult EditProfile()
        {
            if (CurrentUserProfile == null)
            {
                _coreClientService.CreateEmptyUserProfile(User.Identity.Name);
            }
            var userProfile = CurrentUserProfile;
            Guid selectDeptKey = userProfile.DeptId.GetValueOrDefault();
            ViewData["Dept"] = BindDepts(selectDeptKey);
            return View(userProfile);
        }


        //
        // POST: /HOME/EditProfile
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditProfile(string r)
        {
            //UserProfile userProfileToUpdate = new UserProfile()
            //{
            //    Id = new Guid(Request["Id"]),
            //    Name = Request["Name"],
            //    Dept = new Dept() { Id = new Guid(Request["Dept"]) },
            //};
            //EzDeskCoreService.UpdateUserProfile(userProfileToUpdate);

            throw new NotImplementedException();

            //return ReturnView(r, "个人信息修改成功!", null);
        }



        [Authorize]
        public ActionResult Profile(string userId)
        {
            //if (String.IsNullOrEmpty(userId))
            //{
            //    userId = EzDeskCoreService.GetUserProfileByUsername(User.Identity.Name).Key.ToString();
            //}

            //SimpleUser model = EzDeskCoreService.GetSimpleUserById(userId);

            return View();
        }









        //
        // GET: /Home/DeptDetail/Id
        [Authorize]
        public ActionResult DeptDetail(string deptId)
        {
            //var dept = EzDeskCoreService.GetDeptById(deptId);
            //if (deptId == null)
            //{
            //    //TODO: error
            //    return View("Index");
            //}
            //else
            //{
            //    var userInDeptList = EzDeskCoreService.GetUserInDeptList(deptId);
            //    ViewData["UsersInDept"] = userInDeptList;
            //    return View(dept);
            //}
            return null;
        }


        private SelectList BindDepts(Guid selectDeptKey)
        {
            List<SelectListItem> selectList = new List<SelectListItem>();
            var depts = _coreClientService.GetCurrentApps();

            foreach (var dept in depts)
            {
                selectList.Add(
                    new SelectListItem() { Text = dept.Description, Value = dept.Id.ToString() }
                    );
            }
            return new SelectList(selectList, "Value", "Text", selectDeptKey);
        }

        private FeedCollection transformXmlToHtml(string xmlData)
        {
            var items = FeedCollection.Create(xmlData.ToString());
            return items;
        }
    }
}
